﻿Imports LabJack.LabJackUD.U3
Imports USA411.U3Config
Imports System.Data

''' <summary>
''' USA411.LJU3 library. © 2008 ConPro, Inc. all rights reserved.
''' Use of this software in a non-commercial product permitted with clear © credit 
''' Use of this software in a commercial product, by license only 
''' </summary>
''' <remarks></remarks>
Public Class LJU3
    Public Event ChangeStatus(ByVal Action As Integer(), ByVal Status As Boolean())

    Public Enum dPorts
        FIO0 = 0
        FIO1 = 1
        FIO2 = 2
        FIO3 = 3
        FIO4 = 4
        FIO5 = 5
        FIO6 = 6
        FIO7 = 7
        EIO0 = 8
        EIO1 = 9
        EIO2 = 10
        EIO3 = 11
        EIO4 = 12
        EIO5 = 13
        EIO6 = 14
        EIO7 = 15
        CIO0 = 16
        CIO1 = 17
        CIO2 = 18
        CIO3 = 19
    End Enum

#Region "PortDecclarations Here"
    Private WithEvents FIO0 As LJU3Port
    Private WithEvents FIO1 As LJU3Port
    Private WithEvents FIO2 As LJU3Port
    Private WithEvents FIO3 As LJU3Port
    Private WithEvents FIO4 As LJU3Port
    Private WithEvents FIO5 As LJU3Port
    Private WithEvents FIO6 As LJU3Port
    Private WithEvents FIO7 As LJU3Port
    Private WithEvents EIO0 As LJU3Port
    Private WithEvents EIO1 As LJU3Port
    Private WithEvents EIO2 As LJU3Port
    Private WithEvents EIO3 As LJU3Port
    Private WithEvents EIO4 As LJU3Port
    Private WithEvents EIO5 As LJU3Port
    Private WithEvents EIO6 As LJU3Port
    Private WithEvents EIO7 As LJU3Port
    Private WithEvents CIO0 As LJU3Port
    Private WithEvents CIO1 As LJU3Port
    Private WithEvents CIO2 As LJU3Port
    Private WithEvents CIO3 As LJU3Port
#End Region

    Private iHandle As Integer
    Private _Status As Boolean()
    Private _Action As Integer()
    Private _Active As Boolean()
    Private AnyChanges As Boolean = False
    Private _CycleCount As Long = 0



    ReadOnly Property Status() As Boolean()
        Get
            Return _Status
        End Get
    End Property

    Private _Config As DataTable
    Public Property Config() As DataTable
        Get
            Return _Config
        End Get
        Set(ByVal value As DataTable)
            _Config = value
        End Set
    End Property


    Sub New(ByRef pHandle As Integer, ByVal Config As DataTable)
        iHandle = pHandle
        _Config = Config

        ReDim _Status(19)
        ReDim _Action(19)
        ReDim _Active(19)
        InitPorts()
    End Sub

    Public Sub Process()
        AnyChanges = False
        ReDim _Action(19)

        FIO0.Process()
        FIO1.Process()
        FIO2.Process()
        FIO3.Process()
        FIO4.Process()
        FIO5.Process()
        FIO6.Process()
        FIO7.Process()
        EIO0.Process()
        EIO1.Process()
        EIO2.Process()
        EIO3.Process()
        EIO4.Process()
        EIO5.Process()
        EIO6.Process()
        EIO7.Process()
        CIO0.Process()
        CIO1.Process()
        CIO2.Process()
        CIO3.Process()

        If AnyChanges Then RaiseEvent ChangeStatus(_Action, _Status)

    End Sub
    ''' <summary>
    ''' Watchdog
    ''' </summary>
    ''' <returns>CycleCount (long)</returns>
    ''' <remarks>This is a safety routine. If the program calls the watchdog and does 
    ''' not get back a count that is close to the one on the host, then you should start 
    ''' troubleshooting</remarks>
    Public Function Watchdog() As Long
        Return _CycleCount
    End Function





    Private Sub InitPorts()
        FIO0 = New LJU3Port(iHandle, _Config.Rows(0))
        StatActSet(FIO0, 0)
        FIO1 = New LJU3Port(iHandle, _Config.Rows(1))
        StatActSet(FIO1, 1)
        FIO2 = New LJU3Port(iHandle, _Config.Rows(2))
        StatActSet(FIO2, 2)
        FIO3 = New LJU3Port(iHandle, _Config.Rows(3))
        StatActSet(FIO3, 3)
        FIO4 = New LJU3Port(iHandle, _Config.Rows(4))
        StatActSet(FIO4, 4)
        FIO5 = New LJU3Port(iHandle, _Config.Rows(5))
        StatActSet(FIO5, 5)
        FIO6 = New LJU3Port(iHandle, _Config.Rows(6))
        StatActSet(FIO6, 6)
        FIO7 = New LJU3Port(iHandle, _Config.Rows(7))
        StatActSet(FIO7, 7)
        EIO0 = New LJU3Port(iHandle, _Config.Rows(8))
        StatActSet(EIO0, 8)
        EIO1 = New LJU3Port(iHandle, _Config.Rows(9))
        StatActSet(EIO1, 9)
        EIO2 = New LJU3Port(iHandle, _Config.Rows(10))
        StatActSet(EIO2, 10)
        EIO3 = New LJU3Port(iHandle, _Config.Rows(11))
        StatActSet(EIO3, 11)
        EIO4 = New LJU3Port(iHandle, _Config.Rows(12))
        StatActSet(EIO4, 12)
        EIO5 = New LJU3Port(iHandle, _Config.Rows(13))
        StatActSet(EIO5, 13)
        EIO6 = New LJU3Port(iHandle, _Config.Rows(14))
        StatActSet(EIO6, 14)
        EIO7 = New LJU3Port(iHandle, _Config.Rows(15))
        StatActSet(EIO7, 15)
        CIO0 = New LJU3Port(iHandle, _Config.Rows(16))
        StatActSet(CIO0, 16)
        CIO1 = New LJU3Port(iHandle, _Config.Rows(17))
        StatActSet(CIO1, 17)
        CIO2 = New LJU3Port(iHandle, _Config.Rows(18))
        StatActSet(CIO2, 18)
        CIO3 = New LJU3Port(iHandle, _Config.Rows(19))
        StatActSet(CIO3, 19)
    End Sub

    Private Sub StatActSet(ByRef ctl As LJU3Port, ByVal idx As Integer)
        _Status(idx) = ctl.Status
        _Action(idx) = ctl.Active
    End Sub
#Region "State Change Handlers Here"

    Private Sub StateChange(ByVal _PortID As Byte, ByVal ActionID As Integer, ByVal Status As Boolean, ByVal TimeInState As TimeSpan) Handles FIO0.StateChange, FIO1.StateChange, FIO2.StateChange, FIO3.StateChange, FIO4.StateChange, FIO5.StateChange, FIO6.StateChange, FIO7.StateChange, EIO0.StateChange, EIO1.StateChange, EIO2.StateChange, EIO3.StateChange, EIO4.StateChange, EIO5.StateChange, EIO6.StateChange, EIO7.StateChange, CIO0.StateChange, CIO1.StateChange, CIO2.StateChange, CIO3.StateChange
        _Status(_PortID) = Status
        _Action(_PortID) = ActionID

        'With EPack(_PortID)
        '    If status = _Config(_PortID).AlarmState Then ' We are in Alarm State
        '        .Status = status
        '        Select Case _Config(_PortID).AlarmDeviceAction
        '            Case 0 ' Run an Event
        '                .Action = "Execute Event"
        '                .Param1 = _Config(_PortID).AlarmScript
        '            Case 1  ' Direct X10
        '                .Action = "Direct X10"
        '                Select Case _Config(_PortID).AlarmDeviceAction
        '                    Case 0
        '                        .Param1 = "On"
        '                    Case 1
        '                        .Param1 = "Dim"
        '                        .Param2 = "20"
        '                    Case 2
        '                        .Param1 = "Dim"
        '                        .Param2 = "40"
        '                    Case 3
        '                        .Param1 = "Dim"
        '                        .Param2 = "60"
        '                    Case 4
        '                        .Param1 = "Dim"
        '                        .Param2 = "80"
        '                    Case 5
        '                        .Param1 = "Off"
        '                End Select
        '            Case 2 'Set Device Status
        '                .Action = " Set Status"
        '                .Param1 = _Config(_PortID).AlarmDeviceAction
        '            Case 3 ' Set Device Value
        '                .Action = "Set Value"
        '                .Param1 = _Config(_PortID).AlarmDeviceAction
        '            Case Else
        '        End Select
        '    Else 'We are in Safe State
        '        .Status = status
        '        Select Case _Config(_PortID).SafeDeviceAction
        '            Case 0 ' Run an Event
        '                .Action = "Execute Event"
        '                .Param1 = _Config(_PortID).SafeScript
        '            Case 1  ' Direct X10
        '                .Action = "Direct X10"
        '                Select Case _Config(_PortID).SafeDeviceAction
        '                    Case 0
        '                        .Param1 = "On"
        '                    Case 1
        '                        .Param1 = "Dim"
        '                        .Param2 = "20"
        '                    Case 2
        '                        .Param1 = "Dim"
        '                        .Param2 = "40"
        '                    Case 3
        '                        .Param1 = "Dim"
        '                        .Param2 = "60"
        '                    Case 4
        '                        .Param1 = "Dim"
        '                        .Param2 = "80"
        '                    Case 5
        '                        .Param1 = "Off"
        '                End Select
        '            Case 2 'Set Device Status
        '                .Action = " Set Status"
        '                .Param1 = _Config(_PortID).SafeDeviceAction
        '            Case 3 ' Set Device Value
        '                .Action = "Set Value"
        '                .Param1 = _Config(_PortID).SafeDeviceAction
        '            Case Else
        '        End Select
        '    End If
        'End With
        AnyChanges = True
    End Sub
#End Region



End Class


